// JavaScript Document
/*调用方法--设置主窗体*/
	
	var Twindow = {
		bg:function(){
			var html= "<div class=\"ui-mask\" style='height:"+$("body").outerHeight()+"px'></div>";			
			return html;
		},
		title:function(tit){
			var html="<div class=\"ui-window-header\" style=\"border-bottom:0px\"><a action-type=\"window-close\" class=\"ui-window-close\" href=\"javascript:;\">×</a><h3>"+tit+"</h3></div>"
			return html;
		},
		content:function(content){
			var html="<div class=\"ui-window-body\">"+content+"</div>"
			return html;
		},
		button_db:function(){
			var html="<div class=\"ui-window-footer\"><a class=\"ui-window-cancel butn smo-butn  smo-butn-gray\" action-type=\"window-cancel\" href=\"javascript:;\"><i></i>取消</a><a class=\"ui-window-ok butn smo-butn\" action-type=\"window-ok\" href=\"javascript:;\"><i></i>确定</a></div>"
			return html;
		},
		button_d:function(){
			var html="<div class=\"ui-window-footer\"><a class=\"ui-window-ok butn smo-butn smo-butn-orange\" action-type=\"window-ok\" href=\"javascript:;\"><i></i>确定</a></div>"
			return html;
		},
		loginHtml:function(tit,con,db,mask,_width){
			var _that = this;
			var chtml=$("<div class=\"ui-window\"></div>");
			var _body = $("body");
			chtml.append(_that.title(tit));
			chtml.append(_that.content(con));
			switch (db){
				case 1:
					chtml.append(_that.button_d());
					break;
				case 2:
					chtml.append(_that.button_db());
					break;
			}
			_body.append(chtml.show());
			if(mask.length<=0) {
				chtml.after(_that.bg());
			}
			chtml.width(_width);
			var width = chtml.width();
			var height = chtml.height();
			var scroll = $(document).scrollTop();
			chtml.css({
				'margin-left':-width/2,
				'margin-top': -height/2+scroll
			});
			return chtml;
		},
		captchaHtml:function(login_domain){
			var captchaHTML = '<div class="control-group clearfix" id="catcha-div">' +
                                '<label class="control-label" for="inputWarning" style="width: 80px;">验证码：</label>' +
                                '<div requiredtips="请填写验证码" class="controls ui-form-item" data-index="2">' +
                                    '<input type="text" tabindex="3" value="" style="width: 100px" name="catcha" placeholder="验证码">' +
                                '</div>' +
                                '<p class="yzm clearfix">' +
                                    '<input type="hidden" value="523e54f013a3e" name="seed">' +
                                    '<img id="catchaimg" src="'+ login_domain +'/login/verify?seed=523e54f013a3e" class="yzmimg ml5 mr5">' +
                                    '<span>看不清楚？<a href="javascript:void(0);" class="refimg">换一张</a></span>' +
                                '</p>' +
                            '</div>';
            $('#login_form input[name="catcha"]').remove();
            $(captchaHTML).insertBefore($('#login_form input[name="seed"]'));
            $('#login_form > input[name="seed"]').remove();
            return captchaHTML;
		}
	}


	/*构筑主窗体*/
	function popwindow(obj){
		var _mask=$(".ui-mask");
		var _tit=obj.title,
			_con=obj.content,
			_width=obj.width,
			_db=obj.db,
			chtml;
		chtml = Twindow.loginHtml(_tit,_con,_db,_mask,_width);
		if(_mask.length>0) {
			_mask.show();
		}
		edwindow(chtml,obj.ok);
		return chtml;
	}

	/*操作窗体*/
	function edwindow(that,callback){
		var _mask=$(".ui-mask"),_window=that,_close=_window.find(".ui-window-close"),_cancel=_window.find(".ui-window-cancel"),_ok=_window.find(".ui-window-ok");
		_close.click(hideWindow);
		_cancel.click(hideWindow);
		_ok.click(callback);
		function hideWindow(){
			_mask.hide();
			_window.hide();
            $(window).trigger('hideloginwindow');
		}
	};

/*快速注册*/
function aaaa(fun,obj){
    var login_domain = "https://login.zhubajie.com";
    if( document.domain == 'zhubajie.la' ) {
        login_domain = "https://login.zhubajie.la";
    }

    var quickLoginHtml='<style>'
							+'.window-login .login-modo{'
								+'float:left;'
								+'width:50%;'
								+'border-right:1px solid #e5e5e5;'
							+'}'
							+'#work-form{'
								+'float:right;'
								+'width:45%;'
							+'}'
							+'.login-title{'
								+'font-family:"微软雅黑",Microsoft YaHei;'
								+'margin:0 0 25px 20px;'
							+'}'
							+'.login-title span{'
								+'color:#f60;'
								+'margin:0 5px;'+
							'}'
							+'.help-in{'
								+'display:block;'+
							'}'
							+'.ynone{'
								+'display:none;'
							+'}'
						+'</style>'
						+'<div class="window-login clearfix">'
							+'<div class="login-modo">'
								+'<p class="login-title f18">已有<span>猪八戒</span>账号，直接登录</p>'
								+'<form id="login_form">'
						       +' <div class="control-group mt20 clearfix">'
									+'<label class="control-label" for="inputWarning" style="width:80px">账号：</label>'
									+'<div class="controls ui-form-item">'
									+'<input type="text" name="username" placeholder="请输入手机号码/邮箱" tabindex="1">'
									+'<span class="help-inline"></span>'
									+'</div>'
								+'</div>'
								+'<div class="control-group clearfix">'
									+'<label class="control-label" for="inputWarning" style="width:80px">密码：</label>'
									+'<div class="controls ui-form-item">'
									+'<p style="height:42px;"><input type="password" name="password" tabindex="2"></p>'
						           +' <a href="{/get_url rule="/retrieve" domain="login"/}" target="_blank">忘记密码？</a>'
									+'</div>'
								+'</div>'
						       +' <input type="hidden" name="catcha" value="{/$catcha/}">'
						       +' <input type="hidden" name="seed" value="{/$seed/}">'
								+'<input type="hidden" name="q" value="1">'
								+'<div class="control-group clearfix mt30">'
									+'<label class="control-label" for="inputWarning" style="width:82px"></label>'
									+'<div class="controls ui-form-item" type="tel">'
										+'<a class="butn max-butn max-butn-orange" href="###" id="login-zbj"><i></i>立即登录</a>'
										+'<a href="{/get_url rule="/register" domain="login"/}" target="_blank" class="ml10">注册</a>'
									+'</div>'
								+'</div>'
						        +'</form>'
							+'</div>'
							+'<div id="work-form">'
								+'<p class="login-title f16">无需账号，直接发布</p>'
								+'<div class="control-group clearfix mt20" id="nubmer">'
									+'<label class="control-label" for="inputWarning" style="width:100px">手机号码：</label>'
									+'<div class="controls" type="phone" style="width:200px">'
									+'<input type="text" name="mobile" tabindex="3" placeholder="请输入手机号码">'
									+'<span class="help-inline help-in"></span>'
									+'</div>'
								+'</div>'
								+'<div class="control-group ynone clearfix">'
									+'<label class="control-label" for="inputWarning" style="width:100px; padding:0"><a href="###" class="butn butn-gray mt5 show"><i></i>获取验证码</a></label>'
									+'<div class="controls ui-form-item">'
									+'<input type="text" name="yznumber" tabindex="4" style="width:70px">'
									+'<span class="help-inline">验证码将发送到您的手机</span>'
									+'</div>'
								+'</div>'
								+'<div class="control-group clearfix mt30" id="singin">'
									+'<label class="control-label" for="inputWarning" style="width:100px"></label>'
									+'<div class="controls ui-form-item" type="">'
										+'<a class="butn max-butn" href="#" id="quick-pub"><i></i>立刻发布</a>'
									+'</div>'
								+'</div>'
							+'</div>'
						+'</div>';

	var win = null;

    if($(obj).get(0).popwindow){
        $(obj).get(0).popwindow.show();
        $(".ui-mask").show();
        return;
    }

      win = $(obj).get(0).popwindow = new popwindow({
                    title:"&nbsp;",
                    content:quickLoginHtml,
                    width:900
                });
	try{
		// 立刻发布
		quickReleaseModel();
		// 快速登录
		quickLoginModel();
		
		function quickReleaseModel() {
			var _that = this;
			
			$("body").undelegate(".control-group input[name='mobile']","focusout");
			$("body").delegate(".control-group input[name='mobile']","focusout",verifyMobileAvable);

			$("body").undelegate("#quick-pub","click");
			
			$("body").delegate("#quick-pub","click",submitQuickRelease);
		}
		// 验证手机号是否可用
		function verifyMobileAvable() {
			// var _ture=true;
			var login_domain = "https://login.zhubajie.com";
			var _this=$(this);
			var _type=_this.parents(".controls").attr("type");
			if(!_type){
				return false;
			}
			var _val=_this.val();
			var _bool = /^1[34586]\d{9}$/.test(_val);
			if(_bool){
				_this.parents(".control-group").removeClass("error");
				_this.parents(".control-group").addClass("success");
				_this.parents(".controls").find(".help-inline").html(" ");
				// _ture=true;
			}else{
				_this.parents(".control-group").addClass("error");
				// _ture=false;
			}
			if(_type == "phone" && _bool){
				var mobile = _this.val();
				$.ajax({
					type:"post",
					url:login_domain+"/register/ChkUn-type-2.html?mobile="+mobile,
					dataType:"jsonp",
					jsonp:"jsonpcallback",
					success:function(json){
						doVerifyMobileAvable(json);
					}
				});
			}else{
				_this.parents(".control-group").find(".help-inline").html("请输入正确的手机号码");
				_this.parents("#work-form").find(".show").removeClass("butn-green");
				return false;
			}

			function doVerifyMobileAvable(json) {
				if(json.state==1){
					if(_this.parents(".control-group").hasClass("success") == true && _this.parents("#work-form").find(".show").attr("disabled") != "disabled"){
						getYzNumber();
						_this.parents("#work-form").find(".ynone").show();
						_this.parents("#work-form").find(".show").addClass("butn-green");
					}
					_this.parents(".control-group").find(".help-inline").removeClass("help-in");
				}else{
					_this.parents(".control-group").addClass("error");
					_this.parents("#work-form").find(".show").removeClass("butn-green");
					_this.parents(".control-group").find(".help-inline").html(json.msg);	
				}
			}
		}	

		// 获取验证码
		function getYzNumber() {
			$("body").undelegate(".butn-green","click");
			$("body").delegate(".butn-green","click",function(){
				var _this = $(this),
				mobile = $("#work-form").find("input[name='mobile']").val();
				_this.parents("#work-form").find("#quick-pub").addClass("max-butn-green");
				$.ajax({
					type:"post",
					url:login_domain+"/register/quick?send=1&mobile="+mobile,
					dataType:"jsonp",
					jsonp:"jsonpcallback",
					success:function(json){
						if(json.state == 1){
							_this.yzmtimer("<i></i>重新发送");
							ZDK.Tips("发送成功",3000,"success");
						}else{
							ZDK.Tips(json.msg,3000,"error");
						}
					}
				});
			});
		}

		function submitQuickRelease(){
				var _this = $(this),
				_pho=_this.parents("#work-form").find("input[name='mobile']").val(),
				_yzm=_this.parents("#work-form").find("input[name='yznumber']").val();

				if (!verify(_pho,_yzm,1)){
					return false;
				}

				ZDK.module.btnloading({
					obj : _this,
					addClass : "no-int",
					removeClass : "max-butn-green",
					txt : "<i></i>正在发布"
				});	
				$.ajax({
					type:"post",
					url:login_domain+"/register/quick?mobile="+_pho+"&key="+_yzm,
					dataType:"jsonp",
					jsonp:"jsonpcallback",
					success:function(json){
						doSubmitQuickRelease( json );
						ZDK.module.btnloading.reset(_this);
					}
				});
			
			function doSubmitQuickRelease(json){
				if(json.state == 1){
					win.hide();
					$(".ui-mask").hide();
					fun && fun();
				}else{
					ZDK.Tips(json.msg,5000,"error");
				}
			}
		}

		// 立即登录
		function quickLoginModel() {
			$("body").undelegate("#login-zbj","click");
			$("body").delegate("#login-zbj","click",submitQuickLogin);
		}
		
		function submitQuickLogin() {
		// $("body").delegate("#login-zbj","click",function(){	
			var _that = this;		
			var _this = $(this),
				_acc=_this.parents(".login-modo").find("input[name='username']").val(),
				_pwd=_this.parents(".login-modo").find("input[name='password']").val();

			if (!verify(_acc,_pwd,2)) {
				return false;
			}
			ZDK.module.btnloading({
				obj : _this,
				addClass : "no-int",
				removeClass : "max-butn-orange",
				txt : "<i></i>正在登录"
			});	

			$.ajax({
				type:"post",
				url:login_domain+"/login/dologin",
				data:_this.parents("#login_form").serialize(),
				dataType:"jsonp",
				jsonp:"jsonpcallback",
				success:function(json){
					doQuickLogin(json);
					ZDK.module.btnloading.reset(_this);
				}
			});

			function doQuickLogin(json){
				if(json.state == 1){
					win.remove();										
					try{
						if(obj && obj.parents(".ui-window").length > 0){
							obj.parents(".ui-window").show();
						}else{
							$(".ui-mask").remove();
						}
					}catch(e){};
					fun && fun();
				}else{
                    if( json.state == -2 ){
                        document.location.href = json.url;
                    }else{
                        if ( json.state == -3 ) {
                            var tipMsg = '验证码错误!';
                            if( !$('#catcha-div').size() ){
                            	Twindow.captchaHtml(login_domain);
                                tipMsg = '请输入验证码!';
                                refreshImg($(".yzm"));
                            }
                            ZDK.Tips(tipMsg, 5000, "error");
                        } else {
                            ZDK.Tips(json.msg, 5000, "error");
                        }
                        $('#catchaimg').click();
                    }
                }
			}
		}

		function verify(user,password,type){
			if(user=="" || password==""){
				if(type == 1) {
					ZDK.Tips("请输入手机和验证码",3000,"error");
				}
				else if(type == 2) {
					ZDK.Tips("请输入帐号和密码",3000,"error");
				}
			}
			else {
				return true;
			}
		}
	}catch(e){};
};
	